package com.cnbot.aimolib;

import android.graphics.Bitmap;
import android.util.Log;

import com.aimall.core.ImoErrorCode;
import com.aimall.core.define.ImoImageFormat;
import com.aimall.core.define.ImoImageOrientation;
import com.aimall.sdk.extractor.ImoFaceExtractor;
import com.aimall.sdk.extractor.bean.ImoFaceFeature;
import com.aimall.sdk.trackerdetector.ImoFaceTrackerDetector;
import com.aimall.sdk.trackerdetector.bean.ImoFaceInfo;
import com.aimall.sdk.trackerdetector.utils.PointUtils;
import com.cnbot.aimolib.bean.AimoRawFaceData;

import java.util.ArrayList;
import java.util.List;

/**
 * created by ytf
 * 识别核心类
 */
public class DetectionTrackerCore {

	private ImoFaceExtractor imoFaceExtractor;
	private ImoFaceTrackerDetector imoFaceTrackerDetector;

	private boolean hasInitSuccess = false;

	public DetectionTrackerCore(int trackerThreadNum, int detectThreadNum) {
		imoFaceExtractor = new ImoFaceExtractor();
		imoFaceExtractor.configNumThreads(trackerThreadNum);
		int errorCode = imoFaceExtractor.init();
		if (errorCode != ImoErrorCode.IMO_API_RET_SUCCESS) {
			Log.e("s", "ImoFaceExtractor初始化失败");
			return;
		}
		imoFaceTrackerDetector = new ImoFaceTrackerDetector();
		imoFaceTrackerDetector.configDetectNumThreads(detectThreadNum);
		errorCode = imoFaceTrackerDetector.init();
		if (errorCode != ImoErrorCode.IMO_API_RET_SUCCESS) {
			Log.e("s", "ImoFaceTrackerDetector初始化失败");
			return;
		}
		hasInitSuccess = true;
	}

	public boolean canWork(){
		return hasInitSuccess;
	}

	// 获取单张图片的特征值
	public List<AimoRawFaceData> detectFromBmp(Bitmap bitmap) {
		if(!hasInitSuccess) return null;

		imoFaceTrackerDetector.setModel(ImoFaceTrackerDetector.Model.IMO_FACE_TRACKER_DETECTOR_MODEL_PICTURE);
		List<ImoFaceInfo> imoFaceInfos = imoFaceTrackerDetector.execBitmap(bitmap, ImoImageOrientation.IMO_IMAGE_UP);
		return calcFeatureFromBmp(imoFaceInfos, bitmap);
	}

	private List<AimoRawFaceData> calcFeatureFromBmp(List<ImoFaceInfo> imoFaceInfos, Bitmap bmp){
		if (imoFaceInfos != null && imoFaceInfos.size() != 0) {
			float[][] points = PointUtils.getPointFromImoFaceInfo(imoFaceInfos);
			ImoFaceFeature[] imoFaceFeatures = imoFaceExtractor.execBitmap(bmp, points);
			return packResult(imoFaceInfos, imoFaceFeatures);
		}
		return null;
	}

	private List<AimoRawFaceData> packResult(List<ImoFaceInfo> imoFaceInfos, ImoFaceFeature[] imoFaceFeatures) {

		List<AimoRawFaceData> list = new ArrayList<>();
		for (int i = 0; i < imoFaceInfos.size(); ++i) {
			AimoRawFaceData a = new AimoRawFaceData();
			a.faceInfo = imoFaceInfos.get(i);
			a.faceFeature = imoFaceFeatures[i];
			list.add(a);
		}
		return list;
	}

	public List<AimoRawFaceData> detectFromStaticBytes(final byte[] bytes, final int width, int height, ImoImageFormat format, ImoImageOrientation orientation) {
		if(!hasInitSuccess) return null;

		imoFaceTrackerDetector.setModel(ImoFaceTrackerDetector.Model.IMO_FACE_TRACKER_DETECTOR_MODEL_PICTURE);
		List<ImoFaceInfo> list = imoFaceTrackerDetector.execBytes(bytes, width, height, format, orientation);
		if (list != null && list.size() > 0) {
			return calcFeatureFromBytes(list, bytes, width, height, format);
		}
		return null;
	}

	private List<AimoRawFaceData> calcFeatureFromBytes(List<ImoFaceInfo> infos, byte[] bytes,
                                                       final int width, int height, ImoImageFormat format){
		if (infos != null && infos.size() > 0) {
			float[][] points = PointUtils.getPointFromImoFaceInfo(infos);
			ImoFaceFeature[] imoFaceFeatures = imoFaceExtractor.execBytes(bytes, width, height, format, points);
			return packResult(infos, imoFaceFeatures);
		}
		return null;
	}

	// 同步步提取单帧数据特征值
	public List<AimoRawFaceData> detectFromStreamBytes(byte[] bytes, int width, int height, ImoImageFormat format, ImoImageOrientation orientation) {
		if(!hasInitSuccess) return null;

		imoFaceTrackerDetector.setModel(ImoFaceTrackerDetector.Model.IMO_FACE_TRACKER_DETECTOR_MODEL_VIDEO);
		List<ImoFaceInfo> list = imoFaceTrackerDetector.execBytes(bytes, width, height, format, orientation);
		if (list != null && list.size() > 0) {
			return calcFeatureFromBytes(list, bytes, width, height, format);
		}
		return null;
	}

	// 销毁
	public void release() {
		if (imoFaceExtractor != null) {
			imoFaceExtractor.destroy();
			imoFaceExtractor = null;
		}
		if (imoFaceTrackerDetector != null) {
			imoFaceTrackerDetector.destroy();
			imoFaceTrackerDetector = null;
		}
	}

}
